import pygame, math, sys, random 
from belch import Belch
from punch import Punch
from punchR import PunchR

class Priest():

  screenWidth = 800
  screenHeight = 600

  def __init__(self, maxSpeed, pos): 
    self.surface = pygame.image.load("rsc/fatpriest/FPS/fat-priest-8.png")
    self.surface = pygame.transform.scale(self.surface,(27,17))
    self.rect = self.surface.get_rect()
    self.speed = [0,0]
    self.heading = ["n", " "]
    self.maxSpeed = maxSpeed
    self.place(pos)
    self.belching = False
    self.belch = Belch(self)
    self.belching = False
    self.punching = False
    self.punchingR = False
    self.punch = Punch(self)
    self.punchR = PunchR(self)
    self.punchCount = 0
    self.punchRCount = 0
    self.belchCount = 0
    self.belchCoolDown = 0
    self.belchCoolDownMax = 100
    self.maxPunchCount = 10
    self.maxBelchCount = 10
    self.rect.center = pos
    self.radius = ((self.rect.height/2 + self.rect.width/2)/2) 
    self.health = 20
    self.maxHealth = 20
    self.nodamage = 0
    self.living = True

  def modifyHealth (self, amount):    
    self.health += amount
    if self.health <= 0:
        self.health = 0
        self.living = False
    elif self.health >= self.maxHealth:
        self.health = self.maxHealth
          
  def __str__(self):
    return "" + str(self.rect.rect.cent) + str(self.speed)+ str(self.living)
  
  def attack(self, atk):
    if atk == "punch" and self.punchCount == 0:
        self.punching = True
        self.punch.go(self)
        self.punchCount += 1
    if atk == "punchR" and self.punchRCount == 0:
        self.punchingR = True
        self.punchR.go(self)
        self.punchRCount += 1
    if atk == "belch" and self.belchCount == 0 and self.belchCoolDown == 0:
        self.belching = True
        self.belch.go(self)
        self.belchCount += 1
     

  def collide_wall(self, screenWidth, screenHeight):
        if (self.rect.left < 0 
            or self.rect.right > self.screenWidth):
            self.speed[0] = self.speed[0]*-2
            self.move()
            self.speed = [0,0]
            self.didBounce = True
        if (self.rect.top < 0 
            or self.rect.bottom >self.screenHeight):
            self.speed[1] = self.speed[1]*-2
            self.move()
            self.speed = [0,0]
            self.didBounce = True
  
  def move(self):
        self.didBounce = False
        self.rect = self.rect.move(self.speed)
        
        if 0 < self.punchCount < self.maxPunchCount:
            self.punchCount += 1
            self.punch.go(self)
        elif self.punchCount >= self.maxPunchCount:
            self.punchCount = 0
            self.punching = False
            
        if 0 < self.punchRCount < self.maxPunchCount:
            self.punchRCount += 1
            self.punchR.go(self)
        elif self.punchRCount >= self.maxPunchCount:
            self.punchRCount = 0
            self.punchingR = False
            
        if 0 < self.belchCount < self.maxBelchCount:
            self.belchCount += 1
            self.belch.go(self)
        elif self.belchCount >= self.maxBelchCount:
            self.belchCount = 0
            self.belching = False
            self.belchCoolDown = 1
        
        #print self.belchCoolDown
        if 1 <= self.belchCoolDown < self.belchCoolDownMax:
            self.belchCoolDown += 1
        else:
            self.belchCoolDown = 0
    #self.bunchCount += default is 1
  def direction(self, dir):
        if dir == "up":
            self.speed[1] = -self.maxSpeed
        elif dir == "down":
            self.speed[1] = self.maxSpeed
        elif dir == "stop up":
            self.speed[1] = 0
        elif dir == "stop down":
            self.speed[1] = 0
        
        if dir == "right":
            self.speed[0] = self.maxSpeed
        elif dir == "left":
            self.speed[0] = -self.maxSpeed
        elif dir == "stop right":
            self.speed[0] = 0
        elif dir == "stop left":
            self.speed[0] = 0
        #print self.speed
       
    
  def place(self, pos):
    pass
    
  def collide_buildings(self, building):
    pass

  def distToPoint(self, pt):
        x1 = self.rect.center[0]
        x2 = pt[0]
        y1 = self.rect.center[1]
        y2 = pt[1]
        return math.sqrt(((x2-x1)**2)+((y2-y1)**2))
 
  def enemyCollide(self, other, effect):
        if (self.rect.right > other.rect.left 
            and self.rect.left < other.rect.right):
                if (self.rect.bottom > other.rect.top and 
                    self.rect.top < other.rect.bottom):
                    self.hurt = True
                    if self.nodamage == 0:
                        self.modifyHealth(-other.damage)
                        effect.update(self.health, self.maxHealth) 
                        print self.health
                    self.nodamage += 1
                    if self.nodamage == 25:
                        self.nodamage = 0
    
  def collide_point(self, pt, rad):
    if self.distToPoint(pt) <= rad + self.radius:
      return True
    else:
      return False   
      
  '''def collide_priest(self, priest):
    if (self.rect.right > zombie.rect.left and self.rect.left < priest.rect.right):
        if (self.rect.bottom > zombie.rect.top and self.rect.top < priest.rect.bottom):
            if (self.distToPoint(zombie.rect.center) < self.radius + priest.radius):
                if not self.didHit:
                    self.speed[0] = self.speed[0] * -1
                    self.speed[1] = self.speed[1] * -1
                    self.move(None)
                    self.move(None)
                    self.didHit = True
                if not zombie.didHit:
                    zombie.speed[0] = zombie.speed[0] * -1
                    zombie.speed[1] = zombie.speed[1] * -1
                    zombie.didHit = True
                return True
    return False '''
  
  